import { API_CONFIG } from '../../config'
Page({
  data: {
    bgStyle: `background-image: url('${API_CONFIG.staticUrl}/wx-x-mai/questionnaireBg.jpg')`,
    showForm: '', // 下滑样式
    bIsShowContent: true, // 隐藏内容
    bIsShowIcon: false, // 显示向下箭头
    questionnaireData: [], // 获取问卷调查题目列表data
    formIntroduceContent: '', // 小程序介绍内容
    formTitle: '' // 小程序问卷调查名称
  },
  onLoad () {
    this.getQuestionnaireList()
  },
  handleShow (event) {
    const { scrollTop } = event.detail
    if (scrollTop < 250 && this.data.showForm !== '') {
      this.setData({
        showForm: '',
        bIsShowContent: true,
        bIsShowIcon: false
      })
      return
    }

    if (scrollTop > 250 && this.data.showForm !== 'show-form') {
      this.setData({
        showForm: 'show-form',
        bIsShowContent: false,
        bIsShowIcon: true
      })
    }
  },
  /**
   * @see 接口-获取问卷调查题目列表
   */
  getQuestionnaireList () {
    wx.$API['questionnaire/getQuestionnaireList']({}).then((response) => {
      const { title1, title2, questionnaireSubjects } = response.data
      questionnaireSubjects.forEach(itemData => {
        if (itemData.questionType === 2 || itemData.questionType === 3) {
          itemData.checkValueText = itemData.checkValue.split(',') // 返回来的单选、多选的选项是字符串格式，转化为数组
        }
        itemData.value = ''
      })
      this.setData({
        questionnaireData: questionnaireSubjects,
        formIntroduceContent: title1,
        formTitle: title2
      })
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: 'none',
        duration: 5000
      })
    })
  },
  /**
   * @see 接口-提交问卷调查答案
   */
  submitQuestionnaireList () {
    const questionnaireAnswers = []
    this.data.questionnaireData.forEach(item => {
      if (item.value) {
        questionnaireAnswers.push({
          subject: item.subject,
          answer: item.value
        })
      }
    })
    if (questionnaireAnswers.length) {
      wx.$API['questionnaire/submitQuestionnaireList']({ questionnaireAnswers }).then((response) => {
        wx.showToast({
          title: '感谢你的提交',
          duration: 5000
        })
      }).catch((error) => {
        wx.showToast({
          title: error,
          icon: 'none',
          duration: 5000
        })
      })
    } else {
      wx.showToast({
        title: '请填写内容后再提交',
        icon: 'none',
        duration: 5000
      })
    }
  },
  /**
   * @see 多选
   */
  checkboxChange (e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    this.setData({
      [`questionnaireData[${index}].value`]: value.join()
    })
  },
  /**
   * @see 单选
   */
  radioChange (e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    this.setData({
      [`questionnaireData[${index}].value`]: value
    })
  },
  /**
   * @see 简答题
   */
  inputChange (e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    this.setData({
      [`questionnaireData[${index}].value`]: value
    })
  },
  /**
   * @see 点击提交按钮
   */
  handleSubmit () {
    this.submitQuestionnaireList()
  }
})
